var ysb={}; 

layui.use(['form', 'laydate','layer','layedit'], function(){
    var $ = layui.$
    ,element = layui.element
    ,layer = layui.layer
    ,laydate = layui.laydate
    ,form = layui.form;
    var token=localStorage.getToken;
    /**
     * 提交监听
     */
    form.on('submit(saveData)', function(data){
    	console.log(data.field);
    	var id=data.field.id;
    	var name=data.field.name;
    	var groupId=data.field.groupId;
    	var value=data.field.value;
    	var sort=data.field.sort;
    	var remark=data.field.remark;
    	var status=data.field.status;
    	var pathUrl="";
    	if(id==""||id==null){
    		pathUrl=baseApi+"/sys/dict/save";
    	}else{
    		pathUrl=baseApi+"/sys/dict/update";
    	}
        $.ajax({
        	type:"POST",
        	url:pathUrl,
        	data:{"name":name,"token":token,"id":id,"groupId":groupId,"value":value,"sort":sort,"remark":remark,"status":status},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"||msg=="success"){
                     window.parent.ysb.init();//刷新父页面
            		 layer.msg("保存成功");
            		 ysb.closeWind();
            	}else{
            		   layer.msg(data.msg);
            	}
            }
        });
    });
    
    //加载字典组
    ysb.group=function(groupId){
	    $.ajax({
        	type:"GET",
        	url:baseApi+"/sys/dictgroup/queryAll",
        	data:{"token":token},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"&&msg=="success"){
            		 var groupList=data.list;
            		 var strHtml="<select name=\"groupId\" id=\"groupId\" lay-verify=\"required\"><option value=\"\">直接选择或搜索</option>";
            		 for(var i=0;i<groupList.length;i++){
            			 var selected="";
            			 if(groupId!=null){
            				 if(groupList[i].id==groupId){
            					 selected="selected";
            				 }
            			 }
            			 strHtml+="<option "+selected+" value=\""+groupList[i].id+"\">"+groupList[i].name+"</option>"
            		 }
            		 strHtml+="</select>"
            		 $("#groupList").html(strHtml);
            		  form.render('select');
            	}else{
            	    layer.msg(data.msg);
            	}
            }
      }); 
   };
   
   
   
    
    /**
     * 关闭当前的窗口
     */
    ysb.closeWind=function(){
    	var index = parent.layer.getFrameIndex(window.name);
    	parent.layer.close(index);
    };
    //刷新父页面中的树形表格
    ysb.refreshed=function(){
    	var docId =parent.find("iframe")[0].contentWindow.document.getElementById("docId");
    }

   //加载字典详细信息
   ysb.dictInfo=function(id){
	    $.ajax({
        	type:"POST",
        	url:baseApi+"/sys/dict/info/"+id,
        	data:{"token":token},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"||msg=="success"){
            		var dict=data.dict;
            		$("#id").val(ysb.strNull(dict.id));
            		$("#name").val(ysb.strNull(dict.name));
            		$("#value").val(ysb.strNull(dict.value));
            		$("#sort").val(dict.sort);
            		$("#remark").val(dict.remark);
            		ysb.group(dict.groupId);
            		
            	}else{
            	    layer.msg(data.msg);
            	}
            	//清除加载数据
            	localStorage.getId="";
            }
      }); 
   };
   //处理让自己讨厌的字符串（展示在页面上有伤大雅）
   ysb.strNull=function(str){
	   if(str==""||str=="null"||str==null){
		   return "";
	   }else{
		   return str;
	   }
   };
   
   //看是否为编辑
   var id=localStorage.getId;
 //如果是有ID进来，那就认为为修改
   if(id!=null&&id!=""){
	   ysb.dictInfo(id);
   }else{
	   ysb.group(null);
   }
   
  });